<?php
namespace app\common\logic;

use app\common\model\PromGoods;
use app\common\model\Goods;
use think\Db;

/**
* 促销商品逻辑定义
* Class CatsLogic
* @package common\Logic
*/
class PromGoodsLogic extends Prom
{
    protected $promGoods;//促销活动模型
    protected $goods;//商品模型

    public function __construct($goods)
    {
        parent::__construct();
        $this->goods = $goods;

        //活动商品没有规格，活动和规格是一对多
        $this->promGoods = PromGoods::get($goods['prom_id'],'',true);

        if ($this->promGoods) {
            //每次初始化都检测活动是否失效，如果失效就更新活动和商品恢复成普通商品
            if ($this->checkActivityIsEnd() && $this->promGoods['is_end'] == 0) {

                Db::name('goods')->where("id", $this->goods['id'])->update(['prom_type' => 0, 'prom_id' => 0]);

                $this->promGoods->is_end = 1;
                $this->promGoods->save();
                unset($this->goods);
                $this->goods = Goods::get($goods['id'],'',true);
            }
        }
    }

    public function getPromModel(){
        return $this->promGoods;
    }

    /**
     * 活动是否正在进行
     * @return bool
     */
    public function checkActivityIsAble(){
        if(empty($this->promGoods)){
            return false;
        }
        if(time() > $this->promGoods['starttime'] && time() < $this->promGoods['endtime'] && $this->promGoods['status'] == 1 && $this->promGoods['is_end'] == 0){
            return true;
        }
        return false;
    }

    /**
     * 活动是否结束
     * @return bool
     */
    public function checkActivityIsEnd(){
        if(empty($this->promGoods)){
            return true;
        }
        if(time() > $this->promGoods['endtime']){
            return true;
        }
        return false;
    }

    /**
     * 获取商品原始数据
     * @return Goods
     */
    public function getGoodsInfo()
    {
        return $this->goods;
    }

    /**
     * 该活动是否已经失效
     */
    public function IsAble(){
        if(empty($this->promGoods)){
            return false;
        }
        if(time() > $this->promGoods['endtime']){
            return false;
        }
        if($this->promGoods['status'] != 1){
            return false;
        }
        if($this->promGoods['is_end'] == 1){
            return false;
        }
        return true;
    }

    /**
     * 获取商品转换活动商品的数据
     * @return static
     */
    public function getActivityGoodsInfo(){
        $activityGoods = $this->goods;
        $activityGoods['activity_title'] = $this->promGoods['title'];
        $activityGoods['market_price'] = $this->goods['market_price'];
        $activityGoods['starttime'] = $this->promGoods['starttime'];
        $activityGoods['endtime'] =  $this->promGoods['endtime'];
        //有规格

        $activityGoods['shop_price'] = $this->getPromotionPrice($this->goods['shop_price']);
        $activityGoods['description'] =  $this->promGoods['description'];
        return $activityGoods;
    }


    /**
     * 计算促销价格。
     * @param $Price|原价或者规格价格
     * @return float
     * 促销类型:0=直接打折,1=减价优惠,2=固定金额出售
     */
    public function getPromotionPrice($Price){
        switch ($this->promGoods['type']) {
            case 0:
                $promotionPrice = $Price * $this->promGoods['expression'] / 100;//打折优惠
                break;
            case 1:
                $promotionPrice = $Price - $this->promGoods['expression'];//减价优惠
                break;
            case 2:
                $promotionPrice = $this->promGoods['expression'];//固定金额优惠
                break;
            default:
                $promotionPrice = $Price;//原价
                break;
        }
        $promotionPrice = ($promotionPrice >0 ? $promotionPrice : 0); //防止出现负数
        return $promotionPrice;
    }
}
